package com.eims.sls_app.ui;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.toolbox.RequestParams;
import com.contans.view.getHongbaoInfoVo;
import com.contans.view.GetHongbaoLsitVo;
import com.eims.hongbao.fragment.HongbaoListInfoAdapter;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.utils.NetWork;
import com.eims.sls_app.view.XCRoundImageView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import bls.juejin.cn.R;
import progressBar.ProgressDialogBar;

public class HongbaoListInfoActivity extends MyBaseActivity {
	public static final String TAG = "MainHongbaoListActivity";
	private ListView listView;
	private MyHandler myHandler;
	private HongbaoListInfoAdapter adapter;
	private String getHongbaoId, getname, geturl, getdes, getcount;
	private String opt, auth, info, time, imei, crc; // 格式化后的参数

	private ImageButton ibBack;
	private XCRoundImageView ivHead;
	public TextView tvName, tvDes, tvCount, tvDataInfo;
	public getHongbaoInfoVo getInfoVo;
	public DecimalFormat df;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.hongbao_list_info);
		myHandler = new MyHandler();

		Bundle bundle = getIntent().getExtras();
		getHongbaoId = bundle.getString("id");
		getname = bundle.getString("name");
		geturl = bundle.getString("url");
		getdes = bundle.getString("des");

		getcount = bundle.getString("count");

		init();
	}

	public void init() {
		df = new DecimalFormat("######0.00");

		ibBack = (ImageButton) findViewById(R.id.btn_back_con);
		ivHead = (XCRoundImageView) findViewById(R.id.head_img);
		tvName = (TextView) findViewById(R.id.tv_name);
		tvDes = (TextView) findViewById(R.id.tv_des);
		tvCount = (TextView) findViewById(R.id.tv_count);
		tvDataInfo = (TextView) findViewById(R.id.tv_data_info);

		// String name = topicBeans.get(position).name;
		final String isname;
		if (isMobileNO(getname)) {
			isname = getname.substring(0, 3) + "****" + getname.substring(7, getname.length());

		} else {
			isname = getname;

		}

		tvName.setText("来自" + '"' + isname + '"');
		ImageManager.getInstance().displayImage(App.url + geturl, ivHead, ImageManager.getViewsHeadOptions());
		tvDes.setText(getdes);
		tvCount.setText("共" + getcount + "元");
		listView = (ListView) findViewById(R.id.listview);
		ibBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		ivHead.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("userId", getInfoVo.getUid());

				Intent intent = new Intent(HongbaoListInfoActivity.this, CirclePersonInfo.class);
				intent.putExtras(bundle);
				// intent.putExtra("userId", topicBeans.get(position).id+"");
				startActivity(intent);
			}
		});

		if (NetWork.isConnect(this)) {

			ProgressDialogBar.show(this, "加载。。", true);
			MyAsynTask asynTask = new MyAsynTask();
			asynTask.execute();
		} else {
			Toast.makeText(this, "网络连接不可用", Toast.LENGTH_SHORT).show();
		}

	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("1[0-9]{10}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	class MyAsynTask extends AsyncTask<Integer, Integer, String> {

		String error = "-1";

		@Override
		protected String doInBackground(Integer... params) {

			opt = "112";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(HongbaoListInfoActivity.this);
			info = getHongbaoId;// 16
			// info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);

			// String key = MD5.md5(App.user.getUserPass() + App.MD5_key);
			String key = App.key;
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
			String[] values = { opt, auth, info };
			Log.e(TAG, "晒单" + RequestParams.convertParams(HongbaoListInfoActivity.this, opt, info));
			String result = HttpUtils.doPost(App.names, values, App.path);

			Log.e(TAG, "晒单：" + result);

			if (result.contains("error")) {
				error = "-1";
			} else {

				if (result != null) {

					error = "1";
					try {

						getInfoVo = new getHongbaoInfoVo();
						JSONObject jsonObject = new JSONObject(result);

						getInfoVo.total = jsonObject.optString("total");
						getInfoVo.yl = jsonObject.optString("yl");
						getInfoVo.ylje = jsonObject.optString("ylje");
						getInfoVo.syje = jsonObject.optString("syje");
						getInfoVo.uid = jsonObject.optString("uid");
						JSONArray comment = jsonObject.getJSONArray("data");
						List<GetHongbaoLsitVo> commentBeanlists = new ArrayList<GetHongbaoLsitVo>();
						if (!comment.equals("[]")) {

							for (int j = 0; j < comment.length(); j++) {
								JSONObject object2 = comment.getJSONObject(j);
								GetHongbaoLsitVo commentBean = new GetHongbaoLsitVo();
								commentBean.money = object2.optString("money");
								commentBean.date = object2.optString("date");
								commentBean.name = object2.optString("name");
								commentBean.headurl = object2.optString("headurl");
								commentBean.uid = object2.optString("uid");

								commentBeanlists.add(commentBean);
							}
							getInfoVo.data = commentBeanlists;
						}

						Log.e(TAG, "晒单11" + "jixu2" + getInfoVo.toString());
						error = "1";
					} catch (Exception e) {
						error = "-1";
						Log.e(TAG, "晒单11" + e.getMessage());
						e.printStackTrace();
					}

				}

			}

			return error;
		}

		@Override
		protected void onPostExecute(String result) {

			myHandler.sendEmptyMessage(Integer.parseInt(result));

			super.onPostExecute(result);
		}
	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			ProgressDialogBar.stopProgress();
			switch (msg.what) {
			case 5:

				break;
			case -1:
				Toast.makeText(HongbaoListInfoActivity.this, "获取信息失败！", Toast.LENGTH_SHORT).show();
				break;
			case 1:

				if (getInfoVo != null) {
					Double num = Double.parseDouble(getInfoVo.getSyje());
					tvDataInfo.setText(
							"已领取" + getInfoVo.getYl() + "/" + getInfoVo.getTotal() + "个，剩余" + df.format(num) + "元");

					if (getInfoVo.getData() != null) {
						adapter = new HongbaoListInfoAdapter(HongbaoListInfoActivity.this, getInfoVo.getData());

						listView.setAdapter(adapter);
					}

				}

				break;
			case 4:

				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	}
}
